<template>
  <div class="user-header">
    <div class="user-header-right">
      <div class="login-btn" v-if="!token">
        <el-button type="primary" size="mini" @click="$router.push('/login')">登录</el-button>
      </div>
      <div class="user-setting" v-else>
        <strong style="margin-right: 10px;" class="tip">你好，尊贵的<span style="color: #8ebcd3">&nbsp;{{userInfo?.nickname}}&nbsp;</span>先生</strong>
        <el-dropdown @command="handleCommand">
          <el-avatar :src="userInfo?.imgUrl"></el-avatar>
          <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="/details">个人信息</el-dropdown-item>
              <el-dropdown-item command="/password">修改密码</el-dropdown-item>
              <el-dropdown-item command="/login">退出登录</el-dropdown-item>
           </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
export default {
  name: 'UserHeader',
  components: {
  },
  computed: {
    ...mapState('user', ['userInfo']),
    ...mapGetters(['token'])
  },
  methods: {
    handleCommand (command) {
      if (command === '/login') {
        this.$message({
          message: '退出成功',
          type: 'success'
        })
        this.$store.dispatch('user/logoutAction')
      }
      this.$router.push(command)
    }
  }
}
</script>

<style lang="less" scoped>
.user-header {
  position: relative;
  height: 80px;
  text-align: center;
  line-height: 50px;
  background: url(../../assets/big-title.jpg) no-repeat center;
  background-size: contain;
}
.user-header-right{
  position: absolute;
  right: 40px;
  top: 50%;
  transform: translate(0, -50%);
  // border: 1px solid red;
  .user-setting{
    display: flex;
    align-items: center;
    .el-dropdown{
      width: 40px;
      height: 40px;
    }
  }
}
</style>
